package com.compton.UI
{
	import flash.display.DisplayObject;
	import flash.display.Sprite;

	/**
	 * This is the LayoutBox Class. Use this class to build Buttons vertically or horizontally.
	 * Adjust padding between each button and add them to the stage.
	 * @author blakecompton
	 * @example How to use this class. 
	 * <listing version = "3.0">
	 * var _lb:LayoutBox;
	 * _lb = new LayoutBox();
	 * this.addChild(_lb);
	 * 
	 * var btn:Button = new Button();
	 * _lb.addChild(btn);
	 * </listing>
	 */
	
	public class LayoutBox extends Sprite
	{
		private var _children:Array = [];
		private var _padding:int;
		private var _isHorizontal:Boolean;
		
		/**
		 * Creates a new layout box
		 * @param padding Adjust the default padding between objects
		 * @param isHorizontal Adjusts true or false to define if the layout is
		 * horizontal or vertical
		 * 
		 * @example This will demenstrate how to use it in the Main Class, padding first horizontal second
		 * <listing version="3.0">
		 * var lb:LayoutBox = new LayoutBox(10, false);
		 * this.addChild(lb);
		 * </listing>
		 */		
		
		public function LayoutBox(padding:int = 10, isHorizontal:Boolean = false)
		{
			_padding = padding;
			_isHorizontal = isHorizontal;
			super();
		}
		
		/**
		 * Adds a child to the layoutbox 
		 * @param Overrides the addchild movieclip function to add this child to the layoutBox
		 * @return The child that was added
		 * 
		 */		

		override public function addChild(child:DisplayObject):DisplayObject
		{
			super.addChild(child);
			_children.push(child);
			
			updateUI();
			
			return child;
		}
		
		/**
		 * Update UI positions every child of the layoutBox. Incorperates padding and adjust for
		 * horizontal and vertical styles.
		 * 
		 * 
		 */		
		
		public function updateUI():void
		{
			var pos:int = 0;
			
			for each( var child:DisplayObject in _children)
			{
				if(_isHorizontal)
				{
					child.y = 0;
					child.x = pos;
					pos += child.width + _padding;
					
				}else{
					
					child.y = pos;
					child.x = 0;
					pos += child.height + _padding;
				}
			}
		}
		
		/**
		 * This getter and setter determines wether the _isHorizontal value is true or false. After being determined it will 
		 * pass it through the <code>updateUI</code> function to add the child in the appropriate place.
		 * @return returns the boolean true or false to determine whether horizontal or vertical
		 * @example
		 * <listing version = "3.0">
		 * var _lb:LayoutBox;
		 * _lb = new LayoutBox();
		 * this.addChild(_lb);
		 * _lb.isHorizontal = false;
		 * </listing>
		 */		
		
		public function get isHorizontal():Boolean
		{
			return _isHorizontal;
		}
		
		/**
		 * @private
		 */		

		public function set isHorizontal(value:Boolean):void
		{
			_isHorizontal = value;
			updateUI();
		}

		/**
		 * Padding getter setter sets the padding value after the _isHorzontal parameter is determined.
		 * <code>updateUI</code> will update the padding variable.
		 * @return 
		 * @example
		 * <listing version = "3.0">
		 * var _lb = LayoutBox;
		 * _lb = new LayoutBox();
		 * this.addChild(_lb);
		 * _lb.padding = 20;
		 * </listing>
		 */		
		
		public function get padding():int
		{
			return _padding;
		}
		
		/**
		 * @private
		 */		

		public function set padding(value:int):void
		{
			_padding = value;
			updateUI();
		}
	}
}